package andestours.interfaz;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import andestours.mundo.AndesTours;
import andestours.mundo.DestinoValue;
import andestours.mundo.HotelValue;
import andestours.mundo.TransporteValue;

public class ServletAdmin extends ServletTemplate {

	private static final long serialVersionUID = 1L;
	private ArrayList<DestinoValue> destino;
	private ArrayList<HotelValue> hotel;
	private ArrayList<TransporteValue> transporte;

	@Override
	public void imprimirContenido(HttpServletRequest request, PrintWriter out) {
		
		if(request.getParameter("codigo") != null && request.getParameter("nombre") != null &&
				request.getParameter("destino") != null && request.getParameter("costo") != null &&
				request.getParameter("capacidad") != null && request.getParameter("tabla") != null) {
			int codigo = Integer.parseInt(request.getParameter("codigo"));
			String nombre = request.getParameter("nombre");
			int destino = Integer.parseInt(request.getParameter("destino"));
			int costo = Integer.parseInt(request.getParameter("costo"));
			int capacidad = Integer.parseInt(request.getParameter("capacidad"));
			if(request.getParameter("tabla").equals("hotel")) {
				int estrellas = Integer.parseInt(request.getParameter("estrellas"));
				try {
					Date inicio = new Date();
					AndesTours.getInstance().administrarHotel(codigo, nombre, destino, costo, capacidad, estrellas);
					Date fin = new Date();
					System.out.println("RF6 Administrar hotel: " + (fin.getTime() - inicio.getTime()) + " milisegundos.");
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			else if(request.getParameter("tabla").equals("transporte")) {
				String tipo = request.getParameter("tipo");
				String origen = request.getParameter("origen");
				try {
					Date inicio = new Date();
					AndesTours.getInstance().administrarTransporte(codigo, nombre, destino, costo, capacidad, tipo, origen);
					Date fin = new Date();
					System.out.println("RF6 Administrar transporte: " + (fin.getTime() - inicio.getTime()) + " milisegundos.");
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		
		destino = AndesTours.getInstance().darTodosLosDestinos();
		hotel = AndesTours.getInstance().darHoteles();
		transporte = AndesTours.getInstance().darTransportes();
		
		out.println("        <td width='35%' bgcolor='#6666aa'><center> ");
		out.println("                <font face='Verdana, Arial, Helvetica, sans-serif' color='#FF9966'><b>Administrador</b></font> <br /> ");
		out.println("                <br /> ");
		out.println("                <br /> ");
		out.println("                <br /> ");
		out.println("                <br /> ");
		out.println("                <form action='administrador.html' method='post' onsubmit='return validarAdmin()'> ");
		out.println("                    <font face='Verdana, Arial, Helvetica, sans-serif'> ");
		out.println("                    Tabla: <select name='tabla'> ");
		out.println("                        <option value='hotel' onclick='javascript:hotel()'>HOTEL</option> ");
		out.println("                        <option value='transporte' onclick='javascript:transporte()'>TRANSPORTE</option> ");
		out.println("                    </select> ");
		out.println("                    <br /><br /><table><tr><td> ");
		out.println("                    C&oacute;digo <td><input type='text' name='codigo' size='2' maxlength='3' /><tr><td> ");
		out.println("                    Nombre <td><input type='text' name='nombre' maxlength='20' size='15' /><tr><td> ");
		out.println("                    Destino <td><select name='destino'> ");
		for(int i=0; i<destino.size(); i++)
			out.println("                        <option value='" + destino.get(i).getCodigo() + "'>" + destino.get(i).getCodigo() + ": " + destino.get(i).getNombre() + "</option>");
		out.println("                    </select><tr><td> ");
		out.println("                    Costo <td><input type='text' name='costo' size='3' maxlength='4' /><tr><td> ");
		out.println("                    Capacidad <td><input type='text' name='capacidad' size='3' maxlength='4' /><tr><td> ");
		out.println("                    Estrellas <td><select name='estrellas'> ");
		out.println("                        <option>1</option> ");
		out.println("                        <option>2</option> ");
		out.println("                        <option>3</option> ");
		out.println("                        <option>4</option> ");
		out.println("                        <option>5</option> ");
		out.println("                    </select><tr><td> ");
		out.println("                    Tipo <td><select name='tipo' disabled='disabled'> ");
		out.println("                        <option>aereo</option> ");
		out.println("                        <option>terrestre</option> ");
		out.println("                        <option>maritimpo</option> ");
		out.println("                    </select><tr><td> ");
		out.println("                    Origen <td><input type='text' name='origen' maxlength='20' size='15' disabled='disabled' /><tr><td> ");
		out.println("                    </table><br /><br /> ");
		out.println(" 				<input type='submit' value='Modificar / Agregar' /> ");
		out.println("                    </font> ");
		out.println("                </form> ");
		out.println("            </center></td> ");
		out.println("        <td valign='baseline'><font face='Courier New, Courier, monospace' size='1'> ");
		out.println("        	<table><td> ");
		out.println(" 			<table border><thead><center> ");
		out.println(" 			<font face='Verdana, Arial, Helvetica, sans-serif' size='2'>HOTEL</font> ");
		out.println(" 			</center></thead> ");
		out.println(" 				<tr> ");
		out.println(" 					<th>C&oacute;digo</th> ");
		out.println(" 					<th>Nombre</th> ");
		out.println(" 					<th>Destino</th> ");
		out.println(" 					<th>Estrellas</th> ");
		out.println(" 					<th>Habitaciones</th> ");
		out.println(" 					<th>Cupos</th> ");
		out.println(" 					<th>Costo</th> ");
		out.println(" 					<th>Reservas</th> ");
		out.println(" 				</tr> ");
		
		for(int i=0; i<hotel.size(); i++) {
			out.println(" 				<tr> ");
			out.println("					<td>" + hotel.get(i).getCodigo() + "</td>");
			out.println("					<td>" + hotel.get(i).getNombre() + "</td>");
			out.println("					<td>" + hotel.get(i).getDestino() + "</td>");
			out.println("					<td>" + hotel.get(i).getTipo() + "</td>");
			out.println("					<td>" + hotel.get(i).getHabitaciones() + "</td>");
			out.println("					<td>" + hotel.get(i).getCupos() + "</td>");
			out.println("					<td>" + hotel.get(i).getCosto() + "</td>");
			out.println("					<td>" + hotel.get(i).getReservas() + "</td>");
			out.println(" 				</tr> ");
		}
		
		out.println(" 			</table> ");
		out.println(" 			<td>&nbsp;&nbsp;<td><font face='Courier New, Courier, monospace' size='1'> ");
		out.println(" 			<table border><thead><center> ");
		out.println(" 			<font face='Verdana, Arial, Helvetica, sans-serif' size='2'>TRANSPORTE</font> ");
		out.println(" 			</center></thead> ");
		out.println(" 				<tr> ");
		out.println(" 					<th>C&oacute;digo</th> ");
		out.println(" 					<th>Nombre</th> ");
		out.println(" 					<th>Puestos</th> ");
		out.println(" 					<th>Cupos</th> ");
		out.println(" 					<th>Tipo</th> ");
		out.println(" 					<th>Costo</th> ");
		out.println(" 					<th>Destino</th> ");
		out.println(" 					<th>Origen</th> ");
		out.println(" 					<th>Reservas</th> ");
		out.println(" 				</tr> ");
		
		for(int i=0; i<transporte.size(); i++) {
			out.println(" 				<tr> ");
			out.println("					<td>" + transporte.get(i).getCodigo() + "</td>");
			out.println("					<td>" + transporte.get(i).getEmpresa() + "</td>");
			out.println("					<td>" + transporte.get(i).getPuestos() + "</td>");
			out.println("					<td>" + transporte.get(i).getCupos() + "</td>");
			out.println("					<td>" + transporte.get(i).getTipo() + "</td>");
			out.println("					<td>" + transporte.get(i).getCosto() + "</td>");
			out.println("					<td>" + transporte.get(i).getCiudad_Destino() + "</td>");
			out.println("					<td>" + transporte.get(i).getCiudad_Origen() + "</td>");
			out.println("					<td>" + transporte.get(i).getReservas() + "</td>");
			out.println(" 				</tr> ");
		}
		
		out.println(" 			</table> ");
		out.println(" 		</tr></table> ");
		out.println("            </center></td> ");
		out.println("    </tr> ");
	}

}
